package com.axcbd.demo.controller;

import com.axcbd.demo.dto.response.ApiResponse;
import com.axcbd.demo.model.User;
import com.axcbd.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.html.parser.Entity;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ResponseEntity<ApiResponse<User>> register(@RequestBody User user){
        User registeredUser=userService.register(user);
        //return new ResponseEntity<>(user1.toString(), HttpStatus.CREATED);
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(new ApiResponse<>(registeredUser));
    }

    @PostMapping("/login")
    public ResponseEntity<ApiResponse<User>> login(@RequestBody User user){
        User loginUser = userService.login(user.getUserName(), user.getPassword());
        if(loginUser!=null){
            return ResponseEntity.ok(new ApiResponse<>(loginUser));
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ApiResponse<>("账户或密码错误"));
    }
}
